import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:archive/archive.dart';
import 'package:flutter/foundation.dart';
import 'package:heyangkuaidai/FrameWorkModule/NetWork/result_data.dart';
import 'package:heyangkuaidai/FrameWorkModule/hotfix/HtmlZipModel.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;
import 'package:permission_handler/permission_handler.dart';
import 'package:heyangkuaidai/FrameWorkModule/NetWork/http_request.dart';
import 'package:heyangkuaidai/FrameWorkModule/NetWork/address.dart';
class FileManager {

  //获取本地文件
  Future<String> getFile(String filename) async { //fileName 为带后缀的文件名：如：index.html
    return await rootBundle.loadString('sources/$filename');
  }
  Future<String>  getLocalUrl() async{
     Directory docDir =await getApplicationDocumentsDirectory();
     print(docDir.path);
     return docDir.path;
  }
 //读写文件  例：fileName 为带后缀的文件名：如：test.zip  final zipFile = await rootBundle.load('assets/data/test.zip');
  Future <File> _readWriteFile(String fileName, final zipFile,) async{
    Directory docDir= await getApplicationDocumentsDirectory();
    File file = new File('${docDir.path}/$fileName');
    await file.writeAsBytes(zipFile.buffer.asUint8List());
    return file;
  }

  //从本地将zip文件读写到磁盘中进行解压
  Future<String> unZipLocalFile() async{
    WidgetsFlutterBinding.ensureInitialized();
    PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.storage);
     await PermissionHandler().requestPermissions([PermissionGroup.storage]);
    print('permission$permission');
    final zipFile = await rootBundle.load('sources/data/test.zip');
    File file =await _readWriteFile("test.zip", zipFile);
    // 读取zip文件
    final bytes = file.readAsBytesSync();
    // 解压zip文件
    final archive = ZipDecoder().decodeBytes(bytes);
    // 从zip中提取内容归档到磁盘
    Directory docDir = await getApplicationDocumentsDirectory();

    String path;
    for (final file in archive) {
      final filename = file.name;
      if (file.isFile) {
        final data = file.content as List<int>;
        File file1=  new File(docDir.path+'/out/' + filename)
          ..createSync(recursive: true)
          ..writeAsBytesSync(data);
        path =file1.path;
        print('file1.path $path');
      } else {
        new  Directory(docDir.path+'/out/' + filename)
          ..create(recursive: true);
      }
    }
    // File desfile =File(docDir.path+'/out/'+'test/index.html');
    print('docDir.path =$docDir.path');
    return docDir.path;
  }
  Future <String> _dowload()async{
     ResultData res =await HttpManager.getInstance().post(Address.queryZipURL);
     HtmlZipModel responseModel = HtmlZipModel.fromJson(res.data);
     Directory docDir =await getApplicationDocumentsDirectory();
     String savePath =docDir.path+'/out/test.zip';
     await HttpManager.getInstance().download(responseModel.data.url, savePath);
     return savePath;
  }
  //从远程下载到本地的文件目录中解压zip
  Future<String> unZipRemoveFile( ) async {
    WidgetsFlutterBinding.ensureInitialized();
    String soucePath =await _dowload();
    PermissionStatus permission = await PermissionHandler()
        .checkPermissionStatus(PermissionGroup.storage);
    print('permission$permission');
    Map<PermissionGroup,
        PermissionStatus> permissions = await PermissionHandler()
        .requestPermissions([PermissionGroup.storage]);
    File file = File(soucePath);
    // 读取zip文件
    final bytes = file.readAsBytesSync();
    // 解压zip文件
    final archive = ZipDecoder().decodeBytes(bytes);
    // 从zip中提取内容归档到磁盘
    Directory docDir = await getApplicationDocumentsDirectory();
    String path;
    for (final file in archive) {
      final filename = file.name;
      if (file.isFile) {
        final data = file.content as List<int>;
        File file1 = new File(docDir.path + '/out/' + filename)
          ..createSync(recursive: true)
          ..writeAsBytesSync(data);
        path = file1.path;
        print('file1.path $path');
      } else {
        new Directory(docDir.path + '/out/' + filename)
          ..create(recursive: true);
      }
    }
    return docDir.path;
  }
}
